import datetime
import json

from qiniu import Auth

from utils import restful
from .serializers import *


# 获取七牛云token
def get_token(request):
    access_key = 'zyWaoatDF9eaELyOleVFBg3MlZ4XR1W7FzDH5g12'
    secret_key = 'HXlG2FDyAsRA8WurDVZmLp4XvrQpEwAyUxNQy2ck'
    q = Auth(access_key, secret_key)
    token = q.upload_token('laofu-img')

    return restful.ok(data={'token': token})


# 上传作品
def add_works(request):
    name = request.GET.get('name')
    user_id = request.GET.get('user_id')
    url = request.GET.get('url')
    tags = json.loads(request.GET.get('tags'))
    work = Works.objects.create(name=name, user_id_id=user_id, url=url) #在作品表中新增一条记录
    work.tags.add(*tags) #通过作品表相同字段找到分类表，把作品添加到分类表中

    return restful.ok()


# 根据分类查询作品
def get_works_by_tag(request):
    name = request.GET.get('name')
    # result后面的话意思是：作品对象的文件中
    result = Works.objects.filter(tags__name__contains=name)
    data = WorksSerializer(result, many=True).data

    return restful.ok(data=data)


# 根据id获取作品相关的所有信息
def get_works_by_id(request):
    id = request.GET.get('id')
    result = Works.objects.get(id=id)
    data = WorksSerializer(result).data

    return restful.ok(data=data)


# 新增分类
def add_user_tag(request):
    user_id = request.GET.get('user_id')
    name = request.GET.get('name')
    try:
        tag = Tag(user_id_id=user_id, name=name)
        tag.save()
    except:
        return restful.params_error()

    return restful.ok(message='添加分类成功')


# 查询所有分类
def get_tag(request):
    result = Tag.objects.all()

    return restful.ok(data=list(result.values()))


# 删除分类
def delete_user_tag(request):
    id = request.GET.get('id')
    user_id = request.GET.get('user_id')
    tag = Tag.objects.get(id=id, user_id_id=user_id)
    tag.delete()

    return restful.ok()


# 添加留言
def add_comment(request):
    word_id = request.GET.get('id')
    user_id = request.GET.get('user_id')
    message = request.GET.get('message')
    time = datetime.datetime.now()
    comment = Comment(user_id_id=user_id, work_id_id=word_id, message=message, time=time)
    comment.save()

    return restful.ok(message='留言成功')


# 根据作品id获取留言
def get_works_comment(request):
    word_id = request.GET.get('id')
    result = Comment.objects.filter(work_id_id=word_id).order_by('-time')
    data = CommentSerializer(result, many=True).data

    return restful.ok(data=data)


# 获取作品收藏状态
def get_works_collection(request):
    word_id = request.GET.get('id')
    user_id = request.GET.get('user_id')

    collection = Collection.objects.filter(work_id_id=word_id, user_id_id=user_id)

    return restful.ok(data=list(collection.values()))


# 改变收藏状态
def modify_collection(request):
    word_id = request.GET.get('id')
    user_id = request.GET.get('user_id')
    status = request.GET.get('status')
    collection = Collection.objects.get_or_create(user_id_id=user_id, work_id_id=word_id)
    collection[0].status = status
    collection[0].save()

    return restful.ok()


# 获取用户所有作品
def get_works_by_user_id(request):
    user_id = request.GET.get('user_id')
    works = Works.objects.filter(user_id_id=user_id)
    data = WorksSerializer(works, many=True).data

    return restful.ok(data=data)


# 获取用户所有分类
def get_user_tag(request):
    user_id = request.GET.get('user_id')
    result = Tag.objects.filter(user_id_id=user_id)

    return restful.ok(data=list(result.values()))


# 获取用户所有收藏
def get_user_collection(request):
    user_id = request.GET.get('user_id')
    collection = Collection.objects.filter(user_id_id=user_id, status='1')
    data = CollectionSerializer(collection, many=True).data

    return restful.ok(data=data)


def modify_work(request):
    id = request.POST.get('id')
    name = request.POST.get('name')
    tags = json.loads(request.POST.get('tags'))
    tag_list = Tag.objects.filter(id__in=tags)
    work = Works.objects.get(id=id)
    work.tags.clear()
    work.name = name
    work.tags.add(*tags)
    work.save()

    return restful.ok()


def delete_work(request):
    id = request.POST.get('id')
    try:
        Works.objects.get(pk=id).delete()
    except:
        return restful.params_error(message='您已收藏该作品，请先删除收藏')

    return restful.ok()


def delete_collection(request):
    id = request.POST.get('id')
    Collection.objects.get(pk=id).delete()

    return restful.ok()


# 搜索作品
def search_work(request):
    name = request.GET.get('name')
    works = Works.objects.filter(name__contains=name)
    data = WorksSerializer(works, many=True).data

    return restful.ok(data=data)
